package com.atguigu.staservice.service.impl;

import com.atguigu.commonutils.R;
import com.atguigu.staservice.remoteProcedureCall.UcenterClient;
import com.atguigu.staservice.entity.StatisticsDaily;
import com.atguigu.staservice.mapper.StatisticsDailyMapper;
import com.atguigu.staservice.service.StatisticsDailyService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-03-14
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UcenterClient ucenterClient;

    @Autowired
    private StatisticsDailyMapper statisticsDailyMapper;

    @Override
    public void registerCount(String day) {

        //添加记录之前删除表(统计表)相同日期(相同日期、相同日期、相同日期)的数据
        LambdaQueryWrapper<StatisticsDaily> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StatisticsDaily::getDateCalculated, day);
        baseMapper.delete(wrapper);
        System.out.println("删除原有统计数据...");
        //创建统计对象
        //把获取数据添加数据库，统计分析表里面
        StatisticsDaily sta = new StatisticsDaily();
        sta.setDateCalculated(day);
        //1.用户注册数
        R registerR = ucenterClient.countRegister(day);
        Integer countRegister = (Integer) registerR.getData().get("countRegister");
        sta.setUserRegisterNum(countRegister);
        System.out.println("时间" + day + "的用户全部全部注册数为：" + countRegister);

//        Integer registerAllUser = statisticsDailyMapper.getUserRegisiterNum(day);
//        System.out.println("时间" + day + "的用户全部全部注册数为：" + registerAllUser);
        //2.普通用户注册数


        //3.商家用户注册数


        //4.订单创建量


        //5.订单进行中数量


        //6.订单完成量


        //7.订单总的完成交易额


        //8.网站利润统计


//        sta.setRegisterNum(countRegister); //注册人数
//        sta.setDateCalculated(day);//统计日期
//
//        //随机生成其他三项数据
//        sta.setVideoViewNum(RandomUtils.nextInt(100, 2000));
//        sta.setLoginNum(RandomUtils.nextInt(500, 5000));
//        sta.setCourseNum(RandomUtils.nextInt(99, 500));

        baseMapper.insert(sta);
    }

    //图表显示，返回两部分数据，日期json数组，数量json数组
    @Override
    public Map<String, Object> getShowData(String type, String begin, String end) {
        System.out.println(type);
        System.out.println(begin);
        System.out.println(end);
        //根据条件查询对应数据
        LambdaQueryWrapper<StatisticsDaily> wrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(begin) && !StringUtils.isEmpty(end)) {
            wrapper.between(StatisticsDaily::getDateCalculated, begin, end);
        }
//        if (!StringUtils.isEmpty(type)) {
//            switch (type) {
//                case "userRegisterNum":
//                    wrapper
//                    break;
//                case "commonRegisterNum":
//                    wrapper.eq(StatisticsDaily::getCommonRegisterNum, type);
//                    break;
//                case "shopRegisterNum":
//                    wrapper.eq(StatisticsDaily::getShopRegisterNum, type);
//                    break;
//            }
//        }
        List<StatisticsDaily> staList = baseMapper.selectList(wrapper);

        //因为返回有两部分数据：日期 和 日期对应数量
        //前端要求数组json结构，对应后端java代码是list集合
        //创建两个list集合，一个日期list，一个数量list
        List<String> dateCalculatedList = new ArrayList<>();
        List<Integer> numDataList = new ArrayList<>();

        //遍历查询所有数据list集合，进行封装
        for (StatisticsDaily daily : staList) {
            //封装日期list集合
            dateCalculatedList.add(daily.getDateCalculated());
            //封装对应数量
            switch (type) {
                //全部用户
                case "userRegisterNum":
                    numDataList.add(daily.getUserRegisterNum());
                    break;
                //普通用户注册数
                case "commonRegisterNum":
                    numDataList.add(daily.getCommonRegisterNum());
                    break;
                //商家用户注册数
                case "shopRegisterNum":
                    numDataList.add(daily.getShopRegisterNum());
                    break;
                case "orderCreateNum":
                    numDataList.add(daily.getOrderCreateNum());
                    break;
                case "orderRunningNum":
                    numDataList.add(daily.getOrderRunningNum());
                    break;
                case "orderFinishMoneyNum":
                    numDataList.add(daily.getOrderFinishmoneyNum());
                    break;
                case "profitNum":
                    numDataList.add(daily.getProfitNum());
                    break;
                default:
                    break;
            }
        }
        //把封装之后两个list集合放到map集合，进行返回
        Map<String, Object> map = new HashMap<>();
        map.put("dateCalculatedList", dateCalculatedList);
        map.put("numDataList", numDataList);
        return map;
    }
}
